<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>显示部门信息页面</title>
<link href="css/common.css" rel="stylesheet">
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<!-- 可选的Bootstrap主题文件（一般不用引入） -->
<link rel="stylesheet" href="bootstrap/css/bootstrap-theme.min.css">
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="bootstrap/js/jquery.min.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script  src="js/jquery.framewindow.js"></script>
</head>
<body>
<div class="container">
	<div class="header">
		<ol class="breadcrumb">
			<li><a  href="welcome.jsp">首页</a></li>
			<li><a  href="selectDepts.do">部门管理</a></li>
			<li>部门信息</li> 
		</ol>
	</div>
	<div class="article">
		<div class="section action">
			<a class="btn btn-primary" href="pages/addDept.jsp" >新增</a> 
			<a class="btn btn-primary" href="javascript:deleteDepts()">删除</a>
		</div>
		<div style="margin-top: 20px">
			<form id="qryForm" action="selectDepts.do" method="post">
				<input type="hidden" name="pageNo" value="1"> <input
					type="hidden" name="pageSize" value="10">
				<fieldset>
					<label>部门名：
						<input name="key" value="${param.key}">
					</label>
					<button>查询</button>
				</fieldset>
			</form>
		</div>
		<form id="delForm" action="deleteDepts.do" method="post" >
			<strong>${message}</strong>
			<table class="table table-striped">
				<thead>
					<tr>
						<th>NO.</th>
						<th><input type="checkbox" onclick="handleCascadeCheck(this)">
						</th>
						<th>操作</th>
						<th>部门名</th>
						<th>部门经理</th>
						<th>部门下员工数</th>
					</tr>
				</thead>
	
				<c:if test="${result.find}" var="resultFind">
					<tbody>
						<c:forEach var="r" items="${result.rows}"
							varStatus="vs">
							<tr>
								<td>${vs.count}</td>
								<td><input type="checkbox" name="deptId" value="${r.id}">
								</td>
								<td><a name="updateDept">查看/修改</a></td>
								<td>${r.name}</td>
								<td>${r.manager.name}</td>
								<td>${r.members}</td>
							</tr>
						</c:forEach>
					</tbody>
					<tfoot>
						<tr>
							<td colspan="99">
								<div class="section pagin">
									<span> 共<b>${result.totalRecords}</b>条记录 每页<b>${result.pageSize}</b>条
										共<b>${result.totalPages}</b>页
									</span> <span> <c:if test="${result.pageNo==1}">
											<a>首页</a>
											<a>上一页</a>
										</c:if> <c:if test="${result.pageNo!=1}">
											<a href="javascript:goPage(1)">首页</a>
											<a href="javascript:goPage(${result.pageNo-1})">上一页</a>
										</c:if> <input class="tiny" onkeyup="handlePageNoKeyUp()"
										data-totalPages="${result.totalPages}" value="${result.pageNo}">
										<c:if test="${result.pageNo==result.totalPages}">
											<a>下一页</a>
											<a>末页</a>
										</c:if> <c:if test="${result.pageNo!=result.totalPages}">
											<a href="javascript:goPage(${result.pageNo+1})">下一页</a>
											<a href="javascript:goPage(${result.totalPages})">末页</a>
										</c:if>
									</span>
								</div>
							</td>
						</tr>
					</tfoot>
				</c:if>
				<c:if test="${!resultFind}">
					<tr>
						<td>未找到相关数据!</td>
					</tr>
				</c:if>
			</table>
		</form>

	</div>
</div>
	<script type="text/javascript">
		/**
		 * 全选
		 */
		var handleCascadeCheck = function(ckb) {
			var deptIds = document.getElementsByName("deptId");
			for (var i = deptIds.length; i-->0;) {
				deptIds[i].checked = ckb.checked;
			}
		};
		/**
		 * 跳转页面 
		 */
		var goPage = function(no) {
			var qryForm = document.getElementById("qryForm");
			qryForm.pageNo.value = String(no);
			qryForm.submit();
		};
		var handlePageNoKeyUp = function(evt) {
			evt = evt || window.event;
			if (evt.keyCode === 13) {
				var target = evt.target || evt.srcElement;
				var no = parseInt(target.value, 10);
				if (isNaN(no) || no <= 0) {
					no = 1;
				} else {
					var totalPages = parseInt(target
							.getAttribute("data-totalPages"));
					if (no > totalPages) {
						no = totalPages;
					}
				}
				goPage(no);
			}
		};
		/**
		 * 删除 
		 */
		var deleteDepts=function(){
			var deptIds = document.getElementsByName("deptId");
			var checkedDeptIds=[];
			for (var i = deptIds.length; i-->0;) {
				if(deptIds[i].checked){
					checkedDeptIds.push(deptIds[i].value);
				}
			}
			if(checkedDeptIds.length===0){
				alert("请至少选择一条记录");	
				return;
			}
			if(confirm("确认删除?")){
				var delForm=document.getElementById("delForm");
				delForm.submit();	
			}
		};
		$(function(){
			$("a[name='updateDept']").click(function(){
				var name = $(this).parent().next();
				var manager = name.next();
				window.location="showUpdateDeptPage.do?name="+name.text()+"&manager="+manager.text();
			});
		});
	</script>
</body>
</html>